<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "project_single_asset".
 *
 * @property int $id
 * @property int $project_id
 * @property string $asset_type 资产类型
 * @property string $address 地址
 * @property string $area 面积（平米）/数量
 * @property string $price_per_area 市场单价（元）
 * @property string $price_total 预估市场总价
 */
class ProjectSingleAssetDjr extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'project_single_asset';
    }

    /**
     * @return \yii\db\Connection the database connection used by this AR class.
     */
    public static function getDb()
    {
        return Yii::$app->get('forge');
    }

    public function attributes()
    {
        return array_merge(parent::attributes(), ['asset']);
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
//            [['project_id', 'asset_type', 'address', 'area', 'price_per_area', 'price_total'], 'required'],
            [['project_id'], 'integer'],
            [['area', 'price_per_area', 'price_total'], 'number'],
            [['asset_type', 'address'], 'string', 'max' => 255],
            [['area', 'price_per_area'], 'default', 'value' => 0],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'project_id' => 'Project ID',
            'asset_type' => '资产类型',
            'address' => '地址',
            'area' => '面积（平米）/数量',
            'price_per_area' => '市场单价（元）',
            'price_total' => '预估市场总价',
        ];
    }

    /**
     * @inheritdoc
     * @return ProjectSingleAssetDjrQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new ProjectSingleAssetDjrQuery(get_called_class());
    }


    public static function loadMultiple($models, $data, $formName = null)
    {
//        if ($formName === null) {
//            /* @var $first Model|false */
//            $first = reset($models);
//            if ($first === false) {
//                return false;
//            }
//            $formName = $first->formName();
//        }

        $success = false;

        foreach ($models as $i => $model) {
            /* @var $model Model */
            if ($formName == '') {
                if (!empty($data[$i]) && $model->load($data[$i], '')) {
                    $success = true;
                }
            } elseif (!empty($data[$formName]['asset'][$i]) && $model->load($data[$formName]['asset'][$i], '')) {//$data[$formName][$i]

                $success = true;
            }
        }

        return $success;
    }
}
